package javatest;
import java.util.ArrayList;
import java.util.Scanner; 

public class Student2 {
	private ArrayList<Account2> accounts=new ArrayList<>();
	private Scanner sc=new Scanner(System.in);
	
	private void RecordStudentGrades()
	{
		System.out.println("======记录学生成绩======");
		Account2 acc=new Account2();
		boolean a=true;
		boolean c=true;
		
	while(a)
	{
		boolean b=true;
		System.out.println("请输入学生姓名: ");
		String name=sc.next();
	    for(int i=0;i<accounts.size();i++)
	    {
		    Account2 acc1=accounts.get(i);
		    if(acc1.getName().equals(name))
		    {
			System.out.println("该学生姓名已存在,请重新输入!");
			b=false;
			break;
		    }
	    }
	    if(b)
	    {
	    	acc.setName(name);
	    	a=false;
	    }
	}
	
		
	while(c)
	{
		boolean b=true;
		System.out.println("请输入学生学号: ");
		String cardId=sc.next();
	    for(int i=0;i<accounts.size();i++)
	    {
		    Account2 acc1=accounts.get(i);
		    if(acc1.getCardId().equals(cardId))
		    {
			System.out.println("该学生学号已存在,请重新输入!");
			b=false;
			break;
		    }
	    }
	    if(b)
	    {
	    	acc.setCardId(cardId);
	    	c=false;
	    }
	}
		
		System.out.println("请输入课程名称: ");
		String course=sc.next();
		acc.setCourse(course);
		
	while(true)
	{
		System.out.println("请输入成绩(0-100): ");
		double score=sc.nextDouble();
		if(score>=0 && score<=100)
		{
		acc.setScore(score);
		break;
		}
		else
		{
		System.out.println("成绩无效!");
		}
	}
		
		accounts.add(acc);
		System.out.println("成绩已成功记录!");
		
	}
	
	public void StudentName()
	{
		while(true)
		{
		System.out.println("请输入学生姓名: ");
		
		String name=sc.next();
		
		for(int i=0;i<accounts.size();i++)
		{
			Account2 acc=accounts.get(i);
			if(acc.getName().equals(name))
			{
				System.out.println("姓名:"+acc.getName());
				System.out.println("学号:"+acc.getCardId());
				System.out.println("课程:"+acc.getCourse());
				System.out.println("成绩:"+acc.getScore());
				return;
			}
		}
		System.out.println("该学生不存在,您输入的学生姓名有误!");
	    }
	}
	
	public void StudentCardId()
	{
		while(true)
		{
        System.out.println("请输入学生学号: ");
		
		String cardid=sc.next();
		
		for(int i=0;i<accounts.size();i++)
		{
			Account2 acc=accounts.get(i);
			if(acc.getCardId().equals(cardid))
			{
				System.out.println("姓名:"+acc.getName());
				System.out.println("学号:"+acc.getCardId());
				System.out.println("课程:"+acc.getCourse());
				System.out.println("成绩:"+acc.getScore());
				return;
			}
		}
		System.out.println("该学生不存在,您输入的学生学号有误!");
	    }
	}
	
	public void StudentCourse()
	{
		boolean b=true;
		while(b)
		{
        System.out.println("请输入学生课程: ");
		
		String course=sc.next();
		
		boolean a=true;
		
		for(int i=0;i<accounts.size();i++)
		{
			Account2 acc=accounts.get(i);
			if(acc.getCourse().equals(course))
			{
				System.out.println("姓名:"+acc.getName());
				System.out.println("学号:"+acc.getCardId());
				System.out.println("课程:"+acc.getCourse());
				System.out.println("成绩:"+acc.getScore());
				a=false;
				b=false;
			}
		}
		if(a)
		{
		System.out.println("该学生不存在,您输入的学生课程有误!");
		}
		}
	}
	
	public void CheckStudentGrades()
	{
		System.out.println("======查询学生成绩======");
		
		System.out.println("请选择查询方式:");
		
		System.out.println("1.按学生姓名查询");
		System.out.println("2.按学生学号查询");
		System.out.println("3.按课程名称查询");
		
		System.out.println("请输入选项序号: ");
		
		int command=sc.nextInt();
		
		switch(command)
		{
		case 1:		
			StudentName();
			break;
		case 2:         
			StudentCardId();
			break;
		case 3:
			StudentCourse();
			break;
		default:
			System.out.println("您的输入有误");
		}
	}
	
		
	public void start()
	{
		while(true)
		{
			System.out.println("======欢迎使用学生成绩管理系统======");
			System.out.println("请选择操作");
			System.out.println("1.记录学生成绩");
			System.out.println("2.查询学生成绩");
			System.out.println("3.统计课程成绩");
			System.out.println("4.退出系统");
			System.out.println("请输入选项序号: ");
			
			
			int command=sc.nextInt();
			
			switch(command)
			{
			case 1:		
				RecordStudentGrades();
				break;
			case 2:         
				CheckStudentGrades();
				break;
			case 3:
				break;
			case 4:
				System.out.println("感谢使用学生成绩管理系统,再见!");
				return;
			default:
				System.out.println("没有该操作");
			}
		}
	}
}


